package com.mdd.admin.controller.setting;

import com.alibaba.fastjson.JSONObject;
import com.mdd.admin.service.setting.ISettingWebsiteService;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.utils.HttpUtil;
import com.mdd.common.utils.RequestUtil;
import com.mdd.common.utils.YmlUtil;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 网站信息配置管理
 */
@RestController("settingWebsiteController")
@RequestMapping("api/setting/website")
public class WebsiteController {

    @Resource
    ISettingWebsiteService iSettingWebsiteService;

    /**
     * 获取网站配置信息
     *
     * @author fzr
     * @return AjaxResult
     */
    @GetMapping("/detail")
    public AjaxResult detail() {
        Map<String, String> detail = iSettingWebsiteService.detail();
        return AjaxResult.success(detail);
    }

    /**
     * 保存网站配置信息
     *
     * @author fzr
     * @param params 参数
     * @return Object
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Map<String, String> params) {
        String documentInfo = params.getOrDefault("documentInfo", "1");
        if ("2".equals(documentInfo)) {
            String code = YmlUtil.get("like.verify-version.code");
            String server = YmlUtil.get("like.verify-version.server");
            HttpServletRequest request = RequestUtil.handler();
            String domain = request.getServerName();
            String paramStr = "code=" + code + "&domain=" + domain;
            String json = HttpUtil.sendGet(server + "/api/version/productAuth", paramStr);
            JSONObject result = JSONObject.parseObject(json);
            Assert.isTrue("1".equals(result.getString("code")), "调用用监测是否正版授权接口异常");
            JSONObject innerData = result.getJSONObject("data");
            if (innerData != null) {
                Assert.isTrue(innerData.getBoolean("result"), "产品未授权，【文档信息】不可隐藏，需前往官网授权后才能操作");
            }
        }
        iSettingWebsiteService.save(params);
        return AjaxResult.success();
    }

}
